#include <gui/Dx12GlfwGui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_dx12.h>
#include <gfx/dx12/core.h>

namespace primal
{
	namespace gui
	{
		struct
		{
			gfx::ID3D12GraphicsCommandListN* render_list;
			gfx::descriptor_handle font_handle;
		}global1{};

		void impl_new_frame()
		{
			ImGui_ImplDX12_NewFrame();
			ImGui_ImplGlfw_NewFrame();
			ImGui::NewFrame();
		}

		void impl_draw()
		{
			assert(global1.render_list != nullptr);

			ImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(), global1.render_list);
		}

		bool impl_initialize(GLFWwindow* window)
		{
			gfx::srv().allocator(global1.font_handle);
			bool res{};
			res &= ImGui_ImplDX12_Init(gfx::device(), static_cast<int>(gfx::COMMAND_FRAME_COUNT), DXGI_FORMAT_R8G8B8A8_UNORM, gfx::srv().Get(), global1.font_handle.cpu, global1.font_handle.gpu);
			res &= ImGui_ImplGlfw_InitForOther(window, true);
			return res;
		}

		void gui_impl_shutdown()
		{
			ImGui_ImplDX12_Shutdown();
			ImGui_ImplGlfw_Shutdown();
		}

		void set_render_list(gfx::ID3D12GraphicsCommandListN* list)
		{
			global1.render_list = list;
		}
	}
}